<?php

namespace App\Logic;

use App\Model\Project;
use App\Model\ProjectOrder;
use App\Model\User;
use App\Utils\Enum\RedbillEnum;
use App\Utils\Enum\UserEnum;
use Exception;

class AdminRedbillLogic
{
    /**
     * 方案列表
     * @param $input
     * @return array
     */
    public static function listProject($input): array
    {
        [$pageNum, $pageSize, $offset] = parsePageParams($input);
        $query = Project::query()->when(!empty($input['query']['keyword']), function ($query) use ($input) {
            $userIds = User::query()->where(['role_id' => UserEnum::USER_TYPE_EXPERT, 'nickname' => $input['query']['keyword']])->get(['id'])->pluck('id')->toArray();
            $query->whereIn('userid', $userIds);
        })->when(!empty($input['query']['projectId']), function ($query) use ($input) {
            $query->where('project_id', $input['query']['projectId']);
        })->when(!empty($input['query']['sportId']), function ($query) use ($input) {
            $query->where('sport_id', (int)$input['query']['sportId']);
        })->when(!empty($input['query']['matchId']), function ($query) use ($input) {
            $query->where('match_id', $input['query']['matchId']);
        })->when(!empty($input['query']['status']), function ($query) use ($input) {
            $query->where('status', (int)$input['query']['status']);
        })->when(!empty($input['query']['date']), function ($query) use ($input) {
            $query->whereBetween('add_time', [strtotime($input['query']['date']), strtotime($input['query']['date'] . ' 23:59:59')]);
        });
        $total = $query->count();
        if ($total) {
            $records = $query->with(['user'])->orderBy('id', 'desc')->limit($pageSize)->offset($offset)->get()->toArray();
            array_walk($records, function (&$v) {
                $suggest = json_decode($v['suggest_result_code']);
                $labels = [];
                foreach ($suggest as $id) {
                    $labels[] = RedbillEnum::getSuggestTypeName($id);
                }
                $v['suggest_result_labels'] = implode(' ', $labels);
            });
        }
        return [
            'total' => $total,
            'pages' => ceil($total / $pageSize),
            'records' => $records ?? []
        ];
    }

    /**
     * 方案详情
     * @throws Exception
     */
    public static function detailProject($projectId): array
    {
        $project = Project::query()->where('project_id', $projectId)->with(['expert', 'user'])->first();
        if (empty($project)) {
            throw new Exception('方案不存在');
        }
        return $project->toArray();
    }

    /**
     * 方案订单列表
     * @throws Exception
     */
    public static function orderProject($projectId, $input): array
    {
        [$pageNum, $pageSize, $offset] = parsePageParams($input);
        $projectInfo = Project::query()->where('project_id', $projectId)->first();
        if (!$projectInfo) {
            throw new Exception('方案不存在');
        }
        $total = ProjectOrder::query()->where('project_id', $projectId)->count();
        if ($total) {
            $records = ProjectOrder::query()->where('project_id', $projectId)->with(['user'])->limit($pageSize)->offset($offset)->get()->toArray();
        }
        return [
            'total' => $total,
            'pages' => ceil($total / $pageSize),
            'projectInfo' => $projectInfo->toArray(),
            'records' => $records ?? []
        ];
    }
}